package cn.hcbycloud.auth.service;

import cn.hcbycloud.auth.bo.ResourceActionBO;
import cn.hcbycloud.auth.bo.ResourceBO;
import cn.hcbycloud.auth.mapper.ResourceMapper;
import cn.hcbycloud.auth.model.Resource;
import cn.hcbycloud.common.core.utils.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 资源表
 *
 * @author gaoheng
 * @date 2022-01-21 22:50:41
 */
@Service
@AllArgsConstructor
public class ResourceService extends ServiceImpl<ResourceMapper, Resource> {
    private ResourceMapper resourceMapper;

    /**
     * 添加资源
     * @param resourceBo
     * @param tenantId
     * @return
     */
    public String addResourceInfo(ResourceBO resourceBo, String tenantId) {
        Resource resource = null;
        try {
            resource = ObjectUtils.cloneObject(resourceBo, Resource.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        resource.preInsert();
        resource.setTenantId(tenantId);
        resourceMapper.insert(resource);
        return resource.getId();
    }

    /**
     * 根据权限ID列表获取资源行为对象列表
     *
     * @param permissionIds
     * @param tenantId
     * @return
     */
    public List<ResourceActionBO> getResourceActionByPermissionIds(List<String> permissionIds, String tenantId) {
        return resourceMapper.selectResourceActionByPermissionIds(permissionIds, tenantId);
    }

    /**
     * 根据权限编码获取资源行为对象列表
     *
     * @param permissionCodes
     * @param tenantId
     * @return
     */
    public List<ResourceActionBO> getResourceActionByPermissionCodes(List<String> permissionCodes, String tenantId) {
        return resourceMapper.selectResourceActionByPermissionCodes(permissionCodes, tenantId);
    }

}
